import axios from "axios";
import Vue from "vue";
import router from "@/router";

//创建axios实例
const service = axios.create({
    baseURL: "/api",
    timeout: 15000 //请求超时时间
})

//request请求拦截器
service.interceptors.request.use(
    request => {
    return request
},
    error => {
        console.log("错误发生了："+error)
        return Promise.reject(error)
    }
  )

//response 响应拦截器
service.interceptors.response.use(
    response => {
        switch (response.data.data){
            case 40001:
                Vue.prototype.$message({
                    type:'error',
                    message:response.data.message
                })
                router.push({path:'/login'})
                break;
            case 50000:
                Vue.prototype.$message({
                    type:'error',
                    message:response.data.message
                })
                break
        }
        return response
    },
    error => {
        return Promise.reject(error)
    }
)

export default service

